import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class LastChaos_MDL(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(LastChaos_MDL,self).__init__("MDL")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self):
        
        return self.inFile.read_string(self.inFile.read_long())
        
    def parse_MDP2(self, num, meshName):
        
        chunk = self.inFile.read_string(4)
        
        unk1, v1, unk2, v2, unk3, v3 = self.inFile.read_long(6)
        self.inFile.read_long(4)
        
        self.create_face(meshName, num)
        self.add_face_verts(meshName, num, [v1, v2, v3])
        
        
    def parse_IPOL(self, meshName):
        
        count = self.inFile.read_long()
        for i in range(count):
            self.parse_MDP2(i, meshName)
        self.inFile.read_long()
        
    def parse_vertices(self, meshName, numVerts):
        
        for i in range(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            self.inFile.read_long(10)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
        
    def parse_TXV2(self, meshName, size):
        
        numVerts = size // 52
        print(numVerts)
        self.parse_vertices(meshName, numVerts)
        
        count = self.inFile.read_long()
        for i in range(count):
            self.read_name()
            self.inFile.read_long(6) #null
            self.inFile.read_float()
            self.inFile.read_long(2)
            self.inFile.read_long()
            unk1 = self.inFile.read_long()
            self.inFile.seek(4 * unk1, 1)
            unk2 = self.inFile.read_long()
            self.inFile.seek(4 * unk2, 1)
            self.inFile.seek(28, 1)
        self.inFile.read_long(2)
        
    def parse_PTC2(self):
        
        for i in range(31):
            self.inFile.seek(24, 1)
        self.inFile.seek(20, 1)
        
    def parse_STXH(self):
        
        self.inFile.read_long()
        self.inFile.seek(81, 1)
        
    def parse_file(self):
        
        meshName = "temp"
        self.create_object(meshName)
        filesize = self.inFile.filesize()
        
        idstring = self.inFile.read_string(8)
        unk = self.inFile.read_long()
        while self.inFile.tell() != filesize:
            chunk = self.inFile.read_string(4)
            chunkSize = self.inFile.read_long()
            print chunk, chunkSize, self.inFile.tell()
            if chunk == "IPOL":
                self.parse_IPOL(meshName)
            elif chunk == "TXV2":
                self.parse_TXV2(meshName, chunkSize)
            elif chunk == "PTC2":
                self.parse_PTC2()
            elif chunk == "STXH":
                break
            else:
                self.inFile.seek(chunkSize, 1)
            
def read_file(path):
    
    openFile = StructReader(open(path, 'rb'))
    obj = LastChaos_MDL(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def definitions():
    
    return ["MDATV010"], "MDL", "Last Chaos MDL"

if __name__ == '__main__':
    
    file1 = "EM_09_006_1_000.dxg"
    read_file(file1)